Templates Loader Plugins
Template loaders responsible for loading template text from various sources.
Base Template Loader
Reference name base
Base loader loads templates content in templates_dict dictionary using other loader
plugins following this order:
Check if template with given name already exists in
templates_dict, use it if soCheck if template name starts with
ttr://, load it usingttr_template_loaderIf template name references file, load it using
file_template_loaderIf
templatesis a directory load template content usingdir_template_loaderIf
templatesreferring to.xlsxfile load all templates usingxlsx_template_loader
On failure to load template file, base loader will log an error message and TTR will continue processing other data items.
- ttr.plugins.templates.base_template_loader.load(template_name, templates_dict, templates, **kwargs)
Function to locate template file and return it’s content
Attributes
- Parameters
template_name – (str) - name of template to load
templates – (str) - location of templates
templates_dict – (dict) - dictionary of to store template content in
kwargs – (dict) any additional arguments ignored
- Returns
Trueon success andFalseon failure to load template
On success loads template content in
templates_dictand returnsTrue, on failure returnsFalse.
File Template Loader
Reference name file
Loads template for rendering from file.
- ttr.plugins.templates.file_template_loader.load(template_name, templates_dict, filepath=None, **kwargs)
Function to load template content from
filepath.- Parameters
template_name – (str) name of template to load, should point to file if no
filepathargument providedtemplates_dict – (str) dictionary to store template content in
filepath – (str) optional, path to file to open
kwargs – (dict) any additional arguments ignored
- Returns
Trueon success andFalseon failure to load template
Directory Template Loader
Reference name dir
Loads template for rendering from file in directory
- ttr.plugins.templates.dir_template_loader.load(template_name, templates_dict, templates, **kwargs)
Function to load template content from file in directory.
- Parameters
template_name – (str) name of template to load, should point to file
templates_dict – (str) dictionary to store template content in
templates – (str) OS path to directory with template file
kwargs – (dict) any additional arguments ignored
- Returns
Trueon success andFalseon failure to load template
TTR Template Loader
Reference name ttr
Loads templates for rendering from TTR package.
- ttr.plugins.templates.ttr_template_loader.load(template_name, templates_dict, **kwargs)
Function to load template content from
ttr://...path.- Parameters
template_name – (str) name of template to load
templates_dict – (str) dictionary to store template content in
kwargs – (dict) any additional arguments ignored
- Returns
Trueon success andFalseon failure to load template
XLSX Template Loader
Plugin reference name: xlsx
Spreadsheet might contain multiple tabs with names starting with template, these
tabs can contain templates to use for rendering data from other tabs. All the templates
loaded line by line, template:{{ template_name }} lines used to identify end
of previous and start of next template, where template_name used for referencing
template.
Sample table that contains rendering templates, no headers required:
template:interface |
interface {{ interface }} |
description {{ description }} |
template:logging |
logging host {{ log_server }} |
Above templates loaded in a dictionary:
{
"interface": 'interface {{ interface }}\n'
' description {{ description }}',
"logging": 'logging host {{ log_server }}'
}
In this case templates referenced in data using interface and logging template names
- ttr.plugins.templates.xlsx_template_loader.load(templates_dict, templates=None, sheet=None, **kwargs)
Function to load all templates from
xlsxspreadsheets.- Parameters
templates – OS path to
.xlsxfilesheet – openpyxl
sheetobjecttemplates_dict – (dict) dictionary of
{template name: template content}to load templates inkwargs – (dict) any additional arguments ignored
- Returns
Trueon success andFalseon failure to load template